package com.spider.admin.agent.conf;

import com.spider.admin.conf.ENVConf;
import com.spider.admin.utils.CommonUtil;
import com.spider.admin.utils.FileUtils;
import com.spider.admin.utils.OSCommandUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class GlobalConf {
    @Autowired
    private ENVConf envConf;
    private String getHttpConfInfo(){
        return "\n" +
                "#user  spider;\n" +
                "worker_processes  1;\n" +
                "\n" +
                "#error_log  logs/error.log;\n" +
                "#error_log  logs/error.log  notice;\n" +
                "#error_log  logs/error.log  info;\n" +
                "\n" +
                "\n" +
                "#pid        logs/nginx.pid;\n" +
                "\n" +
                "events {\n" +
                "    worker_connections  1024;\n" +
                "}";
    }

    public void writeGlobalConf() {
        try {
            String commonConfPath = null;
            if (OSCommandUtil.isWinOS()) {
                commonConfPath = envConf.getUiContainerBasePath() + "/spider_conf/global.conf";
            } else {
                commonConfPath = envConf.getUiContainerBasePath() + "/nginx/spider_conf/global.conf";
            }
            FileUtils.writeFile(commonConfPath, getHttpConfInfo());
            log.info("生成Global配置");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("HTTP配置生成异常：" + e.getMessage());
        }
    }
}
